<?php
	require 'fpdf.php';
	require '../libraries/UserLibrary.php';
	require '../libraries/CreateReportLib.php';
	session_start();
	isSessionSet("administrator", null, "../index.php");
	
	class PDF extends FPDF{
		var $company = null;
		var $representative = null;
		var $year = null;
		var $rangeMin = null;
		var $rangeMax = null;
		var $address = null;
		var $contactDetails = null;
		var $typeOfSample = null;
		var $typeOfAnalysis = null;
		var $numAnalysis = null;
		var $dateSubmitted = null;
		var $dateAnalyzedStart = null;
		var $today = null;
		var $administrator = null;
		
		//The function sets the paper size to A4, the standard size in all legal documents.
		function PDF($orientation='P',$unit='mm',$format='A4')
		{
			//Call parent constructor
			$this->FPDF($orientation,$unit,$format);
			//Initialization
			$this->B=0;
			$this->I=0;
			$this->U=0;
			$this->HREF='';
		}
		
		function Header()
		{
			$htmlCasl='CENTRAL ANALYTICAL SERVICES LABORATORY';
			$htmlBiotech='National Institute of Molecular Biology and Biotechnology (BIOTECH)';
			$htmlAddressLine1='University of the Phlippines Los Banos';
			$htmlAddressLine2='College, Laguna 4031, Philippines';
			$htmlAddressLine3='Tel/Fax No. (63-49)-536-0587';
			$htmlAddressLine4='Email: casl.biotech.uplb@gmail.com';
			
			$this->SetLineWidth(0.3);
			$this->Line(10, 10, 200, 10);
			$this->SetLineWidth(1);
			$this->Line(10, 11, 200, 11);
			$this->Image('../tools/biotech.jpg',10,15,30,30,'',null);
			$this->Image('../tools/uplb.jpg',167,13,35,33,'',null);
			$this->SetFont('Arial','B',14);
			$this->Cell($this->w-20, 20, $htmlCasl, 0, 0, 'C');
			$this->Ln(5);
			$this->SetFont('Arial','B',10);
			$this->Cell(0, $this->y+5, $htmlBiotech, 0, 0, 'C');
			$this->Ln(5);
			$this->SetFont('Arial',null,10);
			$this->Cell(0, $this->y, $htmlAddressLine1, 0, 0, 'C');
			$this->Ln(3);
			$this->Cell(0, $this->y, $htmlAddressLine2, 0, 0, 'C');
			$this->Ln(3);
			$this->Cell(0, $this->y, $htmlAddressLine3, 0, 0, 'C');
			$this->Ln(3);
			$this->Cell(0, $this->y, $htmlAddressLine4, 0, 0, 'C');
			$this->Line(10, $this->y+20, 200, $this->y+20);
			
			$this->SetXY(10, $this->y+20);
			$this->Cell($this->w-20, 7, "FO-2D4-1.2", 0, 0, 'R');
		}
		
		function GenerateSignatory($y){
			$this->SetXY(10, $y);
			$this->Text(10, $this->y, "Certified by:");
			$this->Text(10, $this->y+15, $this->administrator);
			$this->Text(10, $this->y+19, "OIC, Head Chemist");
		}
		
		function GenerateNote($y){
			$this->SetXY(10, $y);
			$this->SetFont('Arial',null,10);
			$this->Text(10, $y, "Note:");
			$this->Text(20, $y, "Samples were analyzed as received.");
			$this->Text(20, $y+4, "This report shall not be reporduced in part or in full without written permission from this laboratory.");
			$this->Text(20, $y+8, "The use of the BIOTECH seal and name is strictly prohibited unless authorized by proper UPLB authorities.");
			$this->Text(20, $y+12, "This certification is being issued upon the request of ");
			$this->Text(20, $y+16, "      ".$this->company);
			$this->Text(20, $y+20, "      represented by ".$this->representative);
			$this->Text(20, $y+24, "      for purposes of certification on the contents/properties analyzed.");
		}
		
		function Footer(){
			$this->SetFont('Arial',null,8);
			$this->SetXY(10, $this->h-27);
			$this->Cell($this->w-20, 5, $this->company.'/'.$this->year.'-'.$this->rangeMin.'-'.$this->rangeMax.'/Page '.$this->PageNo().' of {nb}', 0, 0, 'R');
			$this->SetLineWidth(1);
			$this->Line(10, $this->h-21, 200, $this->h-21);
			$this->SetLineWidth(0.3);
			$this->Line(10, $this->h-20, 200, $this->h-20);
		}
		
		function PresentInformation(){
			$this->SetXY(10, 70);
			$this->SetFont('Arial',null,10);
			$this->Text(10, $this->y, "Client:");
			$this->Text(10, $this->y+8, "Address:");
			$this->Text(10, $this->y+12, "Contact Details:");
			$this->Text(10, $this->y+16, "Type of Sample:");
			$this->Text(10, $this->y+20, "Types of Analysis:");
			$this->Text(10, $this->y+24, "Number of Samples:");
			$this->Text(10, $this->y+28, "Date Submitted:");
			$this->Text(10, $this->y+32, "Date Analyzed:");
			
			$this->Text(50, $this->y, $this->company);
			$this->Text(50, $this->y+4, $this->representative);
			$this->Text(50, $this->y+8, $this->address);
			$this->Text(50, $this->y+12, $this->contactDetails);
			$this->Text(50, $this->y+16, $this->typeOfSample);
			$this->Text(50, $this->y+20, $this->typeOfAnalysis);
			$this->Text(50, $this->y+24, $this->numAnalysis);
			$this->Text(50, $this->y+28, $this->dateSubmitted);
			$this->Text(50, $this->y+32, $this->dateAnalyzedStart.'-'.$this->today);
		}
		
		function GenerateTable($header, $data){
			//Column widths
			$this->SetXY(10, 110);
		    $w=array(40,25,80,45);
		    //Header
		    for($i=0;$i<count($header);$i++)
		        $this->Cell($w[$i],7,$header[$i],1,0,'C');
		    $this->Ln();
		    //Data
		    foreach($data as $row)
		    {
		        $this->Cell($w[0],6,$row[0], 1, 0, 'C');
		        $this->Cell($w[1],6,$row[1]."-".sprintf("%04d", $row[2]), 1, 0, 'C');
		        $this->Cell($w[2],6,$row[3], 1, 0, 'C');
		        $this->Cell($w[3],6,$row[4]."+/-".$row[5], 1, 0, 'C');
		        $this->Ln();
		    }
		    //Closure line
		    $this->Cell(array_sum($w),0,'','T');
		}
	}
	
	$pdf = new PDF;
	$pdf->AliasNbPages();
	$pdf->SetTitle("CASLIMS");
	$pdf->company = $_SESSION["sample_details"][0][1];
	$pdf->representative = $_SESSION["sample_details"][0][0];
	$pdf->address = $_SESSION["sample_details"][0][2];
	for($i = 3; $i < 7; $i++){
		$current = $_SESSION["sample_details"][0][$i];
		if($current == ""){
			continue;
		}else{
			$pdf->contactDetails = $pdf->contactDetails."/".$current;
		}
	}
	
	$pdf->typeOfSample = $_SESSION["sample_type"][0];
	if(count($_SESSION["sample_type"]) > 1){	
		for ($i = 1; $i < count($_SESSION["sample_types"]); $i++){
			$value = $_SESSION["sample_type"][$i];
			$pdf->typeOfSample = $pdf->typeOfSample.", ".$value;
		}
	}
	
	$pdf->typeOfAnalysis = $_SESSION["sample_analysis"][0];
	if(count($_SESSION["sample_analysis"]) > 1){	
		for ($i = 1; $i < count($_SESSION["sample_analysis"]); $i++){
			$value = $_SESSION["sample_analysis"][$i];
			$pdf->typeOfAnalysis = $pdf->typeOfAnalysis.", ".$value;
		}
	}
	
	$pdf->numAnalysis = count($_SESSION["sample_entry"]);
	$pdf->dateSubmitted = $_SESSION["sample_details"][0][7];
	$pdf->rangeMax = sprintf("%04d", $_SESSION["sample_numbers"][0][2]);
	$pdf->rangeMin = sprintf("%04d", $_SESSION["sample_numbers"][0][1]);
	$pdf->year = $_SESSION["sample_numbers"][0][0];
	$pdf->dateAnalyzedStart = $_SESSION["sample_entry"][0][4];
	$pdf->today = $_SESSION["maxdate"];
	$pdf->administrator = $_SESSION["username"][0][0];
	$header = array("Sample Code", "Lab Number", "Analysis", "Result");
	$pdf->AddPage();
	$pdf->SetXY(10, $pdf->y+7);
	$pdf->SetFont('Arial','BI',14);
	$pdf->Cell($pdf->w-20, 10, "CERTIFICATE OF ANALYSIS", 0, 0, 'C');
	$pdf->PresentInformation();
	$pdf->GenerateTable($header, $_SESSION["information"]);
	$pdf->GenerateNote($pdf->h-80);
	$pdf->GenerateSignatory($pdf->h-50);
	$pdf->Output();
?>